package dao

import (
	"context"
	"fmt"
	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
	"github.com/sirupsen/logrus"
	"watcher/server/config"
)

var influxClient *InfluxClient

func NewInfluxDB(config config.Influxdb) (*InfluxClient, error) {
	if influxClient == nil {
		influxdb := influxdb2.NewClient(fmt.Sprintf("http://%s:%d", config.Host, config.Port), config.Token)
		_, err := influxdb.Health(context.Background())
		if err != nil {
			logrus.Errorf("influxdb error :%v", err)
			return nil, err
		}
		influxClient = &InfluxClient{
			client: influxdb,
			org:    config.Org,
			bucket: config.Bucket,
		}
	}
	return influxClient, nil
}
